package com.stormrage.edu.ctrl.exam;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.exam.ExmStudentExamServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.exam.rep.ExmStudentExamRep;
import com.stormrage.entity.learn.rep.LeaMajorStudyRep;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.student.StuAcct;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: IdxGuideCtrl.java
 * @Package com.stormrage.edu.ctrl.index
 * @Description: 考试 Controller
 * @author www.stormragetech.com
 * @date 2016年4月16日
 * @version V1.0
 */
@Controller
@RequestMapping("/exam")
public class ExmExamCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(ExmExamCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['html.logo']}")
	private String htmlLogo;

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Autowired
	private EduMajorServ eduMajorServ;

	@Autowired
	private ExmStudentExamServ exmStudentExamServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Model model) {

		logger.debug("【 Controller 】-【 IdxGuideCtrl 】-【 index 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			List<LeaMajorStudyRep> array = eduMajorServ.queryListForExam(curr.getId());

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("user", curr);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 IndexCtrl 】-【 index 】-【 GET 】-【 COMPLETED 】");
		return "exam/index";

	}

	@RequestMapping(value = "/{id}/course", method = RequestMethod.GET)
	public String exam(@PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 IdxGuideCtrl 】-【 exam 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaStudentReq req = LeaStudentReq.instance(curr.getId(), id, null);
			List<ExmStudentExamRep> array = exmStudentExamServ.initStudentExamForCourse(req);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("user", curr);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 IndexCtrl 】-【 exam 】-【 GET 】-【 COMPLETED 】");
		return "exam/exam";

	}

	@RequestMapping(value = "/{id}/course", method = RequestMethod.POST)
	public String exam(@PathVariable("id") Integer id, HttpServletRequest request, RedirectAttributes redirect) {

		logger.debug("【 Controller 】-【 IdxGuideCtrl 】-【 exam 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaStudentReq req = LeaStudentReq.instance(curr.getId(), id, null);
			Integer score = exmStudentExamServ.initExamResult(req, request);

			errMsg = "考试成功，得分为" + score;
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = e.getMessage();
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/exam/{id}/course";
		}

		logger.debug("【 Controller 】-【 IndexCtrl 】-【 exam 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/result";

	}

	@RequestMapping(value = "/{id}/mod", method = RequestMethod.GET)
	public String modExam(@PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 IdxGuideCtrl 】-【 exam 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaStudentReq req = LeaStudentReq.instance(curr.getId(), id, null);
			List<ExmStudentExamRep> array = exmStudentExamServ.initStudentExamForModule(req);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("user", curr);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 IndexCtrl 】-【 exam 】-【 GET 】-【 COMPLETED 】");
		return "exam/exam";

	}

	@RequestMapping(value = "/{id}/mod", method = RequestMethod.POST)
	public String modExam(@PathVariable("id") Integer id, HttpServletRequest request, RedirectAttributes redirect) {

		logger.debug("【 Controller 】-【 IdxGuideCtrl 】-【 exam 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaStudentReq req = LeaStudentReq.instance(curr.getId(), id, null);
			Integer score = exmStudentExamServ.initExamResult(req, request);

			errMsg = "考试成功，得分为" + score;
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = e.getMessage();
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/exam/{id}/mod";
		}

		logger.debug("【 Controller 】-【 IndexCtrl 】-【 exam 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/result";

	}

}
